﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class BankDepositsOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }
            }
            else
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                ddlClientIds.DataSource = ClientServices.GetClientID_Name_Collection();
                ddlClientIds.DataTextField = "Other_Names";
                ddlClientIds.DataValueField = "ClientId";
                ddlClientIds.AppendDataBoundItems = true;
                ddlClientIds.DataBind();

                dlBankDeposits.DataSource = BankDepositServices.GetBankDepositCollection();
                dlBankDeposits.DataBind();
            }

        }

        protected void dlBankDeposits_ItemCommand(object source, DataListCommandEventArgs e)
        {
            
            switch (e.CommandName)
            {
                case "EditBankDeposit":
                    dlBankDeposits.SelectedIndex = e.Item.ItemIndex;
                    BankDeposit bd = new BankDeposit();
                    bd.ClientId = ((Label)dlBankDeposits.SelectedItem.FindControl("lblClientId")).Text;
                    bd.BankId = ((Label)dlBankDeposits.SelectedItem.FindControl("lblBankId")).Text;
                    bd.AccNo = ((Label)dlBankDeposits.SelectedItem.FindControl("lblAccNo")).Text;
                    Session["BankDeposit"] = bd;
                    Response.Redirect("~/Admin/BankDeposits.aspx");
                    break;
            }
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["BankDeposit"] = null;
            Response.Redirect("~/Admin/BankDeposits.aspx");
        }

        protected void btnCalcAccrued_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in dlBankDeposits.Items)
            {
                Label lblAccNo = (Label)item.FindControl("lblAccNo");
                AccuredInterestServices.CalculateAccruedInterest(lblAccNo.Text, DateTime.Today);
            }
            
            Response.Redirect("~/Admin/BankDepositsOverview.aspx");

        }

        protected void ddlClientIds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlClientIds.SelectedValue != string.Empty)
            {
                dlBankDeposits.DataSource = BankDepositServices.GetBankDepositCollectionByClientID(ddlClientIds.SelectedValue);
                dlBankDeposits.DataBind();
            }
            else
            {
                dlBankDeposits.DataSource = BankDepositServices.GetBankDepositCollection();
                dlBankDeposits.DataBind();
            }
        }
    }
}
